{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT License.\n",
    "\n",
    "# Chain network CyberBattle Gym played by a random agent"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Gym random agent attacking a chain-like network\n",
    "\n",
    "## Chain network\n",
    "We consider a computer network of Windows and Linux machines where each machine has vulnerability \n",
    "granting access to another machine as per the following pattern:\n",
    "\n",
    "    Start ---> (Linux ---> Windows --->  ... Linux ---> Windows)*  ---> Linux[Flag]\n",
    "\n",
    "The network is parameterized by the length of the central Linux-Windows chain.\n",
    "The start node leaks the credentials to connect to all other nodes:\n",
    "\n",
    "For each `XXX ---> Windows` section, the XXX node has:\n",
    "    -  a local vulnerability exposing the RDP password to the Windows machine\n",
    "    -  a bunch of other trap vulnerabilities (high cost with no outcome)\n",
    "For each `XXX ---> Linux` section,\n",
    "    - the Windows node has a local vulnerability exposing the SSH password to the Linux machine\n",
    "    - a bunch of other trap vulnerabilities (high cost with no outcome)\n",
    "\n",
    "The chain is terminated by one node with a flag (reward)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Benchmark\n",
    "The following plot shows the average and one standard deviation cumulative reward over time as a random agent attacks the network."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%HTML\n",
    "<img src=\"random_plot.png\" width=\"300\">"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "import logging\n",
    "import gym\n",
    "from gym import spaces\n",
    "import numpy as np\n",
    "import networkx as nx\n",
    "import cyberbattle.simulation.actions as actions\n",
    "import cyberbattle._env.cyberbattle_env as cyberbattle_env\n",
    "import cyberbattle.agents.random_agent as random_agent\n",
    "import cyberbattle.samples.chainpattern.chainpattern as chainpattern\n",
    "import importlib\n",
    "importlib.reload(actions)\n",
    "importlib.reload(cyberbattle_env)\n",
    "importlib.reload(chainpattern)\n",
    "\n",
    "logging.basicConfig(stream=sys.stdout, level=logging.ERROR, format=\"%(levelname)s: %(message)s\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# chainpattern.create_network_chain_link(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "gym_env = gym.make('CyberBattleChain-v0', size=10, attacker_goal=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "gym_env.environment"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "gym_env.environment.network.nodes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "gym_env.action_space"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "gym_env.action_space.sample()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "gym_env.observation_space.sample()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "outputPrepend"
    ]
   },
   "outputs": [],
   "source": [
    "for i in range(100) : gym_env.sample_valid_action()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false,
    "tags": [
     "outputPrepend"
    ]
   },
   "outputs": [],
   "source": [
    "random_agent.run_random_agent(1, 10000, gym_env)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "o,r,d,i = gym_env.step(gym_env.sample_valid_action())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "o"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}